/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.MapUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.service.DavServlet;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.DavNames;
/*     */ import com.zimbra.cs.mailbox.DavNames.DavName;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Message.CalendarItemInfo;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.StringTokenizer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class UrlNamespace
/*     */ {
/*     */   public static final String ATTACHMENTS_PREFIX = "/attachments";
/*     */   public static final String PRINCIPALS = "principals";
/*     */   public static final String PRINCIPAL_USERS = "users";
/*     */   public static final String PRINCIPALS_PATH = "/principals/users/";
/*     */   public static final String ACL_USER = "/principals/users/";
/*     */   public static final String ACL_GUEST = "/principals/guests/";
/*     */   public static final String ACL_GROUP = "/principals/groups/";
/*     */   public static final String ACL_COS = "/principals/cos/";
/*     */   public static final String ACL_DOMAIN = "/principals/domain/";
/*     */   
/*     */   public static UrlComponents parseUrl(String url)
/*     */   {
/*  76 */     UrlComponents uc = new UrlComponents();
/*     */     
/*  78 */     int index = url.indexOf("/dav");
/*  79 */     if (index >= 0) {
/*  80 */       url = url.substring(index + "/dav".length());
/*  81 */       int delim = url.indexOf('/', 1);
/*  82 */       if (delim > 0) {
/*  83 */         uc.user = url.substring(1, delim);
/*  84 */         url = url.substring(delim);
/*     */       }
/*     */     }
/*  87 */     uc.path = url;
/*  88 */     return uc;
/*     */   }
/*     */   
/*     */   public static Collection getCollectionAtUrl(DavContext ctxt, String url) throws DavException
/*     */   {
/*  93 */     UrlComponents uc = parseUrl(url);
/*  94 */     int lastPos = uc.path.length() - 1;
/*  95 */     if (uc.path.endsWith("/"))
/*  96 */       lastPos--;
/*  97 */     int index = uc.path.lastIndexOf('/', lastPos);
/*     */     String path;
/*  99 */     String path; if (index == -1) {
/* 100 */       path = "/";
/*     */     } else
/* 102 */       path = uc.path.substring(0, index);
/* 103 */     String user = uc.user;
/* 104 */     if (user == null)
/* 105 */       user = ctxt.getUser();
/* 106 */     DavResource rsc = getResourceAt(new DavContext(ctxt, path), user, path);
/* 107 */     if ((rsc instanceof Collection))
/* 108 */       return (Collection)rsc;
/* 109 */     throw new DavException("invalid uri", 404, null);
/*     */   }
/*     */   
/*     */   public static DavResource getResourceAtUrl(DavContext ctxt, String url) throws DavException
/*     */   {
/* 114 */     if (url.indexOf("/principals/users/") >= 0)
/* 115 */       return getPrincipalAtUrl(ctxt, url);
/* 116 */     UrlComponents uc = parseUrl(url);
/* 117 */     if ((uc.user == null) || (uc.path == null))
/* 118 */       throw new DavException("invalid uri", 404, null);
/* 119 */     DavResource rs = getResourceAt(ctxt, uc.user, uc.path);
/* 120 */     if (rs != null)
/* 121 */       rs.mUri = uc.path;
/* 122 */     return rs;
/*     */   }
/*     */   
/*     */   public static DavResource getPrincipalAtUrl(DavContext ctxt, String url) throws DavException {
/* 126 */     ZimbraLog.dav.debug("getPrincipalAtUrl");
/* 127 */     String name = ctxt.getAuthAccount().getName();
/* 128 */     if (url != null) {
/* 129 */       int index = url.indexOf("/principals/users/");
/* 130 */       if ((index == -1) || (url.endsWith("/principals/users/")))
/*     */         try {
/* 132 */           return new Principal(ctxt.getAuthAccount(), url);
/*     */         } catch (ServiceException se) {
/* 134 */           throw new DavException("invalid uri", 404, se);
/*     */         }
/* 136 */       index += "/principals/users/".length();
/* 137 */       name = url.substring(index);
/* 138 */       if (name.indexOf('/') > 0)
/* 139 */         name = name.substring(0, name.indexOf('/'));
/* 140 */       ZimbraLog.dav.debug("name: " + name);
/*     */     } else {
/* 142 */       url = "/";
/*     */     }
/*     */     try
/*     */     {
/* 146 */       Account a = Provisioning.getInstance().get(Key.AccountBy.name, name);
/* 147 */       if (a == null)
/* 148 */         throw new DavException("user not found", 404, null);
/* 149 */       return new User(ctxt, a, url);
/*     */     } catch (ServiceException se) {
/* 151 */       throw new DavException("user not found", 404, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static DavResource getPrincipal(DavContext ctxt, Account acct) throws DavException {
/*     */     try {
/* 157 */       return new User(ctxt, acct, getPrincipalUrl(acct.getName()));
/*     */     } catch (ServiceException se) {
/* 159 */       throw new DavException("user not found", 404, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static DavResource getResourceAt(DavContext ctxt, String user, String path) throws DavException
/*     */   {
/* 165 */     ZimbraLog.dav.debug("getResource at user='%s' path='%s'", new Object[] { user, path });
/* 166 */     if (path == null) {
/* 167 */       throw new DavException("invalid uri", 404, null);
/*     */     }
/*     */     
/* 170 */     java.util.Collection<DavResource> rss = getResources(ctxt, user, path, false);
/* 171 */     if (rss.size() > 0) {
/* 172 */       return (DavResource)rss.iterator().next();
/*     */     }
/* 174 */     return null;
/*     */   }
/*     */   
/*     */   public static java.util.Collection<DavResource> getResources(DavContext ctxt, String user, String path, boolean includeChildren)
/*     */     throws DavException
/*     */   {
/* 180 */     ArrayList<DavResource> rss = new ArrayList();
/* 181 */     if (user.equals("")) {
/*     */       try {
/* 183 */         rss.add(new Principal(ctxt.getAuthAccount(), "/dav"));
/* 184 */         return rss;
/*     */       }
/*     */       catch (ServiceException e) {}
/*     */     }
/*     */     
/* 189 */     String target = path.toLowerCase();
/* 190 */     DavResource resource = null;
/*     */     
/* 192 */     if (target.startsWith("/attachments")) {
/* 193 */       resource = getPhantomResource(ctxt, user);
/*     */     } else {
/*     */       try {
/* 196 */         resource = getMailItemResource(ctxt, user, path);
/*     */       } catch (ServiceException se) {
/* 198 */         if ((path.length() == 1) && (path.charAt(0) == '/') && (se.getCode().equals("service.PERM_DENIED")))
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/* 203 */           rss.add(new Collection("/", user));
/* 204 */           ctxt.setCollectionPath("/");
/* 205 */           if (includeChildren) {
/*     */             try {
/* 207 */               rss.addAll(getFolders(ctxt, user));
/*     */             } catch (ServiceException e) {
/* 209 */               ZimbraLog.dav.warn("can't get folders for user='%s'", user, e);
/*     */             }
/*     */           }
/* 212 */           return rss;
/*     */         }
/* 214 */         ZimbraLog.dav.warn("can't get mail item resource for user='%s' path='%s'", user, path, se);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 219 */     if (resource != null) {
/* 220 */       rss.add(resource);
/*     */     }
/* 222 */     if ((resource != null) && (includeChildren)) {
/* 223 */       rss.addAll(resource.getChildren(ctxt));
/*     */     }
/*     */     
/* 226 */     return rss;
/*     */   }
/*     */   
/*     */   public static DavResource getResourceByItemId(DavContext ctxt, String user, int id) throws ServiceException, DavException
/*     */   {
/* 231 */     MailItem item = getMailItemById(ctxt, user, id);
/* 232 */     return getResourceFromMailItem(ctxt, item);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String getAclUrl(String principal, String type)
/*     */     throws DavException
/*     */   {
/* 247 */     Account account = null;
/* 248 */     Provisioning prov = Provisioning.getInstance();
/*     */     try {
/* 250 */       account = prov.get(Key.AccountBy.id, principal);
/* 251 */       StringBuilder buf = new StringBuilder();
/* 252 */       buf.append(type);
/* 253 */       if (account != null) {
/* 254 */         buf.append(account.getName());
/*     */       } else
/* 256 */         buf.append(principal);
/* 257 */       return getAbsoluteUrl(null, buf.toString());
/*     */     } catch (ServiceException e) {
/* 259 */       throw new DavException("cannot create ACL URL for principal " + principal, 500, e);
/*     */     }
/*     */   }
/*     */   
/*     */   public static String getRawResourceUrl(DavResource rs) {
/* 264 */     return "/dav/" + rs.getOwner() + rs.getUri();
/*     */   }
/*     */   
/*     */ 
/*     */   public static String getResourceUrl(DavResource rs)
/*     */   {
/* 270 */     String str = HttpUtil.urlEscape(getRawResourceUrl(rs));
/* 271 */     return str.replaceAll("//", "%2F");
/*     */   }
/*     */   
/*     */   public static String getPrincipalUrl(Account account) {
/* 275 */     return getPrincipalUrl(account, account);
/*     */   }
/*     */   
/*     */   private static boolean onSameServer(Account thisOne, Account thatOne) {
/* 279 */     if (thisOne.getId().equals(thatOne.getId()))
/* 280 */       return true;
/*     */     try {
/* 282 */       Provisioning prov = Provisioning.getInstance();
/* 283 */       Server mine = prov.getServer(thisOne);
/* 284 */       Server theirs = prov.getServer(thatOne);
/* 285 */       if ((mine != null) && (theirs != null))
/* 286 */         return mine.getId().equals(theirs.getId());
/*     */     } catch (Exception e) {
/* 288 */       ZimbraLog.dav.warn("can't get domain or server for %s %s", thisOne.getId(), thatOne.getId(), e);
/*     */     }
/* 290 */     return true;
/*     */   }
/*     */   
/* 293 */   public static String getPrincipalUrl(Account authAccount, Account targetAccount) { String url = getPrincipalUrl(targetAccount.getName());
/* 294 */     if (!onSameServer(authAccount, targetAccount)) {
/*     */       try {
/* 296 */         url = getAbsoluteUrl(targetAccount, url);
/*     */       } catch (ServiceException se) {
/* 298 */         ZimbraLog.dav.warn("can't generate absolute url for " + targetAccount.getName(), se);
/*     */       }
/*     */     }
/* 301 */     return url;
/*     */   }
/*     */   
/* 304 */   public static String getPrincipalUrl(String user) { return HttpUtil.urlEscape("/principals/users/" + user + "/"); }
/*     */   
/*     */   public static String getPrincipalCollectionUrl(Account acct) throws ServiceException
/*     */   {
/* 308 */     return HttpUtil.urlEscape("/principals/users/");
/*     */   }
/*     */   
/*     */   public static String getSchedulingInboxUrl(String authUser, String user) {
/* 312 */     StringBuilder url = new StringBuilder();
/*     */     
/* 314 */     url.append("/dav").append("/").append(authUser).append("/Inbox/");
/* 315 */     if (!authUser.equals(user)) {
/* 316 */       url.append(user).append("/");
/*     */     }
/* 318 */     return url.toString();
/*     */   }
/*     */   
/*     */   public static String getSchedulingOutboxUrl(String authUser, String user) {
/* 322 */     StringBuilder url = new StringBuilder();
/*     */     
/* 324 */     url.append("/dav").append("/").append(authUser).append("/Sent/");
/* 325 */     if (!authUser.equals(user)) {
/* 326 */       url.append(user).append("/");
/*     */     }
/* 328 */     return url.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static String getFolderUrl(String authUser, String folder)
/*     */   {
/* 335 */     StringBuilder url = new StringBuilder();
/* 336 */     url.append("/dav").append("/").append(authUser).append("/").append(folder).append("/");
/* 337 */     return url.toString();
/*     */   }
/*     */   
/*     */   public static String getResourceUrl(Account user, String path) throws ServiceException {
/* 341 */     return getAbsoluteUrl(user, "/dav/" + user.getName() + path);
/*     */   }
/*     */   
/*     */   private static String getAbsoluteUrl(Account user, String path) throws ServiceException {
/* 345 */     Provisioning prov = Provisioning.getInstance();
/* 346 */     Domain domain = null;
/* 347 */     Server server = prov.getLocalServer();
/* 348 */     if (user != null) {
/* 349 */       domain = prov.getDomain(user);
/* 350 */       server = prov.getServer(user);
/*     */     }
/* 352 */     return DavServlet.getServiceUrl(server, domain, path);
/*     */   }
/*     */   
/* 355 */   private static Map<Pair<String, String>, Pair<DavResource, Long>> sRenamedResourceMap = MapUtil.newLruMap(100);
/*     */   
/*     */   public static void addToRenamedResource(String user, String path, DavResource rsc) {
/* 358 */     synchronized (sRenamedResourceMap) {
/* 359 */       sRenamedResourceMap.put(new Pair(user, path.toLowerCase()), new Pair(rsc, Long.valueOf(System.currentTimeMillis())));
/*     */     }
/*     */   }
/*     */   
/*     */   public static DavResource checkRenamedResource(String user, String path) {
/* 364 */     Pair<String, String> key = new Pair(user, path.toLowerCase());
/* 365 */     DavResource rsc = null;
/* 366 */     synchronized (sRenamedResourceMap) {
/* 367 */       Pair<DavResource, Long> item = (Pair)sRenamedResourceMap.get(key);
/* 368 */       if (item != null) {
/* 369 */         long age = System.currentTimeMillis() - ((Long)item.getSecond()).longValue();
/*     */         
/* 371 */         if (age > 900000L) {
/* 372 */           sRenamedResourceMap.remove(key);
/*     */         } else
/* 374 */           rsc = (DavResource)item.getFirst();
/*     */       }
/*     */     }
/* 377 */     return rsc;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static DavResource getMailItemResource(DavContext ctxt, String user, String path)
/*     */     throws ServiceException, DavException
/*     */   {
/* 389 */     Provisioning prov = Provisioning.getInstance();
/* 390 */     Account account = prov.get(Key.AccountBy.name, user);
/* 391 */     if (account == null) {
/* 392 */       throw new DavException("no such account " + user, 404, null);
/*     */     }
/* 394 */     if ((ctxt.getUser().compareTo(user) != 0) || (!Provisioning.onLocalServer(account))) {
/*     */       try {
/* 396 */         return new RemoteCollection(ctxt, path, account);
/*     */       } catch (MailServiceException.NoSuchItemException e) {
/* 398 */         return null;
/*     */       }
/*     */     }
/*     */     
/* 402 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 403 */     int id = 0;
/* 404 */     int index = path.indexOf('?');
/* 405 */     if (index > 0) {
/* 406 */       Map<String, String> params = HttpUtil.getURIParams(path.substring(index + 1));
/* 407 */       path = path.substring(0, index);
/* 408 */       if (params.containsKey("id")) {
/*     */         try {
/* 410 */           id = Integer.parseInt((String)params.get("id"));
/*     */         }
/*     */         catch (NumberFormatException e) {}
/*     */       }
/*     */     }
/*     */     
/* 416 */     OperationContext octxt = ctxt.getOperationContext();
/* 417 */     MailItem item = null;
/*     */     
/*     */ 
/* 420 */     if (path.equals("/")) {
/* 421 */       item = mbox.getFolderByPath(octxt, "/");
/* 422 */     } else if (id > 0) {
/* 423 */       item = mbox.getItemById(octxt, id, MailItem.Type.UNKNOWN);
/*     */     }
/* 425 */     if (item != null) {
/* 426 */       return getResourceFromMailItem(ctxt, item);
/*     */     }
/*     */     try
/*     */     {
/* 430 */       return getResourceFromMailItem(ctxt, mbox.getItemByPath(octxt, path));
/*     */ 
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException e)
/*     */     {
/* 435 */       DavResource rs = checkRenamedResource(user, path);
/* 436 */       if (rs != null) {
/* 437 */         return rs;
/*     */       }
/*     */       
/* 440 */       if (path.endsWith("/")) {
/* 441 */         path = path.substring(0, path.length() - 1);
/*     */       }
/* 443 */       index = path.lastIndexOf('/');
/* 444 */       String folderPath = path.substring(0, index);
/* 445 */       String baseName = path.substring(index + 1);
/* 446 */       Folder f = null;
/* 447 */       if (index != -1) {
/*     */         try {
/* 449 */           f = mbox.getFolderByPath(octxt, folderPath);
/*     */         }
/*     */         catch (MailServiceException.NoSuchItemException e) {}
/*     */       }
/* 453 */       if (f != null)
/*     */       {
/*     */ 
/* 456 */         DavNames.DavName davName = null;
/* 457 */         if (DebugConfig.enableDAVclientCanChooseResourceBaseName) {
/* 458 */           davName = DavNames.DavName.create(mbox.getId(), f.getId(), baseName);
/*     */         }
/* 460 */         if (davName != null) {
/* 461 */           Integer itemId = DavNames.get(davName);
/* 462 */           if (itemId != null) {
/* 463 */             item = mbox.getItemById(octxt, itemId.intValue(), MailItem.Type.UNKNOWN);
/* 464 */             if ((item != null) && (f.getId() == item.getFolderId())) {
/* 465 */               return getResourceFromMailItem(ctxt, item);
/*     */             }
/* 467 */             item = null;
/*     */           }
/*     */         }
/* 470 */         if (baseName.toLowerCase().endsWith(".ics")) {
/* 471 */           String uid = baseName.substring(0, baseName.length() - ".ics".length());
/*     */           
/* 473 */           uid = HttpUtil.urlUnescape(uid);
/* 474 */           index = uid.indexOf(',');
/* 475 */           if (index > 0) {
/*     */             try {
/* 477 */               id = Integer.parseInt(uid.substring(index + 1));
/*     */             }
/*     */             catch (NumberFormatException e) {}
/*     */           }
/* 481 */           if (id > 0) {
/* 482 */             item = mbox.getItemById(octxt, id, MailItem.Type.UNKNOWN);
/*     */           } else {
/* 484 */             item = mbox.getCalendarItemByUid(octxt, uid);
/*     */           }
/* 486 */           if ((item != null) && (f.getId() != item.getFolderId())) {
/* 487 */             item = null;
/*     */           }
/* 489 */         } else if (baseName.toLowerCase().endsWith(".vcf")) {
/* 490 */           rs = AddressObject.getAddressObjectByUID(ctxt, baseName, account, f);
/* 491 */           if (rs != null) {
/* 492 */             return rs;
/*     */           }
/* 494 */         } else if ((f.getId() == 2) || (f.getId() == 5)) {
/* 495 */           ctxt.setActingAsDelegateFor(baseName);
/*     */           
/* 497 */           return getResourceFromMailItem(ctxt, f);
/*     */         }
/*     */       }
/*     */     }
/* 501 */     return getResourceFromMailItem(ctxt, item);
/*     */   }
/*     */   
/*     */   private static java.util.Collection<DavResource> getFolders(DavContext ctxt, String user) throws ServiceException, DavException {
/* 505 */     Provisioning prov = Provisioning.getInstance();
/* 506 */     Account account = prov.get(Key.AccountBy.name, user);
/* 507 */     if (account == null) {
/* 508 */       throw new DavException("no such account " + user, 404, null);
/*     */     }
/* 510 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 511 */     OperationContext octxt = ctxt.getOperationContext();
/* 512 */     ArrayList<DavResource> rss = new ArrayList();
/* 513 */     for (Folder f : mbox.getVisibleFolders(octxt))
/* 514 */       rss.add(getResourceFromMailItem(ctxt, f));
/* 515 */     return rss;
/*     */   }
/*     */   
/*     */   private static DavResource getCalendarItemForMessage(DavContext ctxt, Message msg) throws ServiceException {
/* 519 */     Mailbox mbox = msg.getMailbox();
/* 520 */     if ((msg.isInvite()) && (msg.hasCalendarItemInfos())) {
/* 521 */       Message.CalendarItemInfo calItemInfo = msg.getCalendarItemInfo(0);
/*     */       try {
/* 523 */         Invite invite = calItemInfo.getInvite();
/* 524 */         if ((invite == null) && (calItemInfo.calItemCreated()))
/*     */         {
/* 526 */           CalendarItem item = mbox.getCalendarItemById(ctxt.getOperationContext(), calItemInfo.getCalendarItemId());
/*     */           
/* 528 */           invite = calItemInfo.getInvite();
/* 529 */           int compNum = calItemInfo.getComponentNo();
/* 530 */           invite = item.getInvite(msg.getId(), compNum);
/*     */         }
/* 532 */         if (invite != null) {
/* 533 */           String path = CalendarObject.CalendarPath.generate(ctxt, msg.getPath(), invite.getUid(), Integer.valueOf(mbox.getId()), Integer.valueOf(msg.getId()), msg.getId());
/*     */           
/* 535 */           return new CalendarObject.ScheduleMessage(ctxt, path, ctxt.getUser(), invite, msg);
/*     */         }
/*     */       }
/*     */       catch (MailServiceException.NoSuchItemException e) {}
/*     */     }
/*     */     
/*     */ 
/* 542 */     return null;
/*     */   }
/*     */   
/*     */   public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException
/*     */   {
/* 547 */     DavResource resource = null;
/* 548 */     if (item == null) {
/* 549 */       return resource;
/*     */     }
/* 551 */     MailItem.Type itemType = item.getType();
/*     */     try
/*     */     {
/*     */       MailItem.Type viewType;
/* 555 */       switch (itemType) {
/*     */       case MOUNTPOINT: 
/* 557 */         Mountpoint mp = (Mountpoint)item;
/* 558 */         viewType = mp.getDefaultView();
/*     */         
/* 560 */         if ((!ctxt.useIcalDelegation()) && (viewType == MailItem.Type.APPOINTMENT)) {
/* 561 */           resource = new RemoteCalendarCollection(ctxt, mp);
/*     */         } else {
/* 563 */           resource = new RemoteCollection(ctxt, mp);
/*     */         }
/* 565 */         break;
/*     */       case FOLDER: 
/* 567 */         Folder f = (Folder)item;
/* 568 */         viewType = f.getDefaultView();
/* 569 */         if ((f.getId() == 2) && (DavResource.isSchedulingEnabled())) {
/* 570 */           resource = new ScheduleInbox(ctxt, f);
/* 571 */         } else if ((f.getId() == 5) && (DavResource.isSchedulingEnabled())) {
/* 572 */           resource = new ScheduleOutbox(ctxt, f);
/* 573 */         } else if ((viewType == MailItem.Type.APPOINTMENT) || (viewType == MailItem.Type.TASK)) {
/* 574 */           resource = getCalendarCollection(ctxt, f);
/* 575 */         } else if (viewType == MailItem.Type.CONTACT) {
/* 576 */           resource = new AddressbookCollection(ctxt, f);
/*     */         } else {
/* 578 */           resource = new Collection(ctxt, f);
/*     */         }
/* 580 */         break;
/*     */       case DOCUMENT: 
/* 582 */         resource = new Notebook(ctxt, (Document)item);
/* 583 */         break;
/*     */       case APPOINTMENT: 
/*     */       case TASK: 
/* 586 */         resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem)item);
/* 587 */         break;
/*     */       case MESSAGE: 
/* 589 */         resource = getCalendarItemForMessage(ctxt, (Message)item);
/* 590 */         break;
/*     */       case CONTACT: 
/* 592 */         resource = new AddressObject(ctxt, (Contact)item);
/*     */       }
/*     */     }
/*     */     catch (ServiceException e) {
/* 596 */       resource = null;
/* 597 */       ZimbraLog.dav.info("cannot create DavResource", e);
/*     */     }
/* 599 */     return resource;
/*     */   }
/*     */   
/*     */   private static MailItemResource getCalendarCollection(DavContext ctxt, Folder f) throws ServiceException, DavException {
/* 603 */     String[] homeSets = Provisioning.getInstance().getConfig().getMultiAttr("zimbraCalendarCalDavAlternateCalendarHomeSet");
/*     */     
/*     */ 
/* 606 */     if ((homeSets.length > 0) && ((f.getId() == 10) || (f.getId() == 15))) {
/* 607 */       return new Collection(ctxt, f);
/*     */     }
/* 609 */     if ((f.getDefaultView() == MailItem.Type.APPOINTMENT) && (!ctxt.getAuthAccount().isFeatureCalendarEnabled())) {
/* 610 */       return new Collection(ctxt, f);
/*     */     }
/* 612 */     if ((f.getDefaultView() == MailItem.Type.TASK) && (!ctxt.getAuthAccount().isFeatureTasksEnabled())) {
/* 613 */       return new Collection(ctxt, f);
/*     */     }
/* 615 */     return new CalendarCollection(ctxt, f);
/*     */   }
/*     */   
/*     */   private static DavResource getPhantomResource(DavContext ctxt, String user) throws DavException
/*     */   {
/* 620 */     String target = ctxt.getPath();
/*     */     
/* 622 */     ArrayList<String> tokens = new ArrayList();
/* 623 */     StringTokenizer tok = new StringTokenizer(target, "/");
/* 624 */     int numTokens = tok.countTokens();
/* 625 */     while (tok.hasMoreTokens()) {
/* 626 */       tokens.add(tok.nextToken());
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     DavResource resource;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     DavResource resource;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     DavResource resource;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 653 */     switch (numTokens) {
/*     */     case 1: 
/*     */     case 2: 
/* 656 */       resource = new BrowseWrapper(target, user, tokens);
/* 657 */       break;
/*     */     case 3: 
/* 659 */       if (((String)tokens.get(1)).equals("by-date")) {
/* 660 */         resource = new SearchWrapper(target, user, tokens);
/*     */       } else
/* 662 */         resource = new BrowseWrapper(target, user, tokens);
/* 663 */       break;
/*     */     case 4: 
/* 665 */       if (((String)tokens.get(1)).equals("by-date")) {
/* 666 */         resource = new Attachment(target, user, tokens, ctxt);
/*     */       } else
/* 668 */         resource = new SearchWrapper(target, user, tokens);
/* 669 */       break;
/*     */     case 5: 
/* 671 */       resource = new Attachment(target, user, tokens, ctxt);
/* 672 */       break;
/*     */     default: 
/* 674 */       resource = null;
/*     */     }
/*     */     
/* 677 */     return resource;
/*     */   }
/*     */   
/*     */   private static MailItem getMailItemById(DavContext ctxt, String user, int id) throws DavException, ServiceException {
/* 681 */     Provisioning prov = Provisioning.getInstance();
/* 682 */     Account account = prov.get(Key.AccountBy.name, user);
/* 683 */     if (account == null) {
/* 684 */       throw new DavException("no such account " + user, 404, null);
/*     */     }
/* 686 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 687 */     return mbox.getItemById(ctxt.getOperationContext(), id, MailItem.Type.UNKNOWN);
/*     */   }
/*     */   
/*     */   public static Account getPrincipal(String principalUrl) throws ServiceException {
/* 691 */     int index = principalUrl.indexOf("/principals/users/");
/* 692 */     if (index == -1)
/* 693 */       return null;
/* 694 */     String acct = principalUrl.substring(index + "/principals/users/".length());
/* 695 */     Provisioning prov = Provisioning.getInstance();
/* 696 */     return prov.get(Key.AccountBy.name, acct);
/*     */   }
/*     */   
/*     */   public static class UrlComponents
/*     */   {
/*     */     public String user;
/*     */     public String path;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/UrlNamespace.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */